clear;clc;
clear persistent
clear global
abs_start_time=getsecs;
gogogo=true;
KbQueueCreate();
KbQueueStart();
KbQueueFlush();
ListenChar(2);% warnings sent but still needed
lastbutton=0;
ts=[];
bt=[];
figure(34543);clf
hdot=plot(0,0,'or');
set(hdot,'MarkerSize',12)
axis([-100 100 -100 100]);
drawnow;
while gogogo
    [keyPress, keyCodes, keyTimes]= Kb_bcoe(abs_start_time);
    if keyPress
        if keyCodes(1)==27
            disp('done')
            gogogo=false;
        else
            keyCodes %#ok<NOPTS>
        end
    end
    joyin=jst;%get sidewinder info live. no buffer=no time stamp
    if joyin(5)~=0
        if lastbutton~=joyin(5)%anti-hammering
            lastbutton=joyin(5);
            ts(end+1)=(getsecs-abs_start_time)*1000; %#ok<SAGROW>
            bt(end+1)=joyin(5);%#ok<SAGROW>
            if  bitand(lastbutton,1)
                set(hdot,'Marker','+');
            end
            if  bitand(lastbutton,2)
                set(hdot,'Marker','o');
            end
            if  bitand(lastbutton,4)
                set(hdot,'Marker','s');
            end
            if  bitand(lastbutton,8)
                set(hdot,'Marker','x');
            end
            if  bitand(lastbutton,16)
                set(hdot,'color',[1 0 0]);
            end
            if  bitand(lastbutton,32)
                set(hdot,'color',[0 0 1]);
            end
            if lastbutton==48
                set(hdot,'XData',0,'YData',0)
            end
            figure(34543)
            drawnow;
            
        end
    else
        lastbutton=0;
    end
    xyadj=round(joyin([2,1])*10)/10;
    if any(xyadj~=0)
        XData=get(hdot,'XData')+xyadj(1);
        YData=get(hdot,'YData')+xyadj(2);
        if XData> 99,XData= 99;end
        if XData<-99,XData=-99;end
        if YData> 99,YData= 99;end
        if YData<-99,YData=-99;end
        set(hdot,'XData',XData,'YData',YData)
        figure(34543)
        drawnow;
    end
    
end
set(hdot,'XData',0,'YData',0)
clf
KbQueueStop();
KbQueueRelease();
ListenChar(0);

